{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 4.7 范例：随机漫步 Example: Random Walks\r\n",
    "\r\n",
    " "
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 绪  论"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 利用 Python 的内建模块 random 来编写\r\n",
    "import random\r\n",
    "import matplotlib.pyplot\r\n",
    "\r\n",
    "\r\n",
    "position = 0\r\n",
    "walk_1 = [position]\r\n",
    "nsteps = 1000\r\n",
    "\r\n",
    "for i in list(range(nsteps)):    # 书中的 xrange() 函数在Python3中已经取消，可以用 list(range()) 实现相同的功能\r\n",
    "    # step = 1 if (random.randint(0,1)) else -1\r\n",
    "    if (random.randint(0,1)):\r\n",
    "        step = 1\r\n",
    "    else:\r\n",
    "        step = -1\r\n",
    "    position += step\r\n",
    "    walk_1.append(position)\r\n",
    "\r\n",
    "matplotlib.pyplot.plot(walk_1, linewidth=0.5)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 利用NmuPy构建随机漫步\r\n",
    "import numpy\r\n",
    "from dependency import arr_info, arr_stat\r\n",
    "\r\n",
    "nsteps = 1000\r\n",
    "\r\n",
    "draws = numpy.random.randint(0, 2, size=nsteps)\r\n",
    "steps = numpy.where(draws > 0, 1, -1)\r\n",
    "walk_2 = steps.cumsum()\r\n",
    "\r\n",
    "matplotlib.pyplot.plot(walk_2, linewidth=0.5)\r\n",
    "\r\n",
    "arr_info([draws, walk_2])\r\n",
    "arr_stat(walk_2)\r\n",
    "\r\n",
    "# 首次穿越时间\r\n",
    "print(\"首次到达10的步数:\", (numpy.abs(walk_2) > 10).argmax() )    # 注意：这里利用绝对值函数abs()实现了对+10、-10的同时检测"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 一次模拟多个随机漫步"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [
    "# 模拟 5000 次随机漫步的过程\r\n",
    "import numpy\r\n",
    "import matplotlib.pyplot\r\n",
    "from dependency import arr_info, arr_stat\r\n",
    "\r\n",
    "\r\n",
    "nwalks = 5   # 随机漫步的总次数\r\n",
    "nsteps = 1000   # 每次随机漫步的步数\r\n",
    "\r\n",
    "draws = numpy.random.randint(low=0, high=2, size=(nwalks, nsteps))\r\n",
    "# draws = numpy.random.normal(loc=0, scale=0.25, size=(nwalks, nsteps))  # 可以利用其他类型的分布来生成该数据\r\n",
    "\r\n",
    "steps = numpy.where(draws > 0, 1, -1)\r\n",
    "walks = steps.cumsum(1)\r\n",
    "\r\n",
    "arr_info([draws, steps, walks])\r\n",
    "arr_stat(walks)\r\n",
    "\r\n",
    "x_value = numpy.arange(1, nsteps+1)\r\n",
    "matplotlib.pyplot.figure(dpi=300)\r\n",
    "for i in range(nwalks):\r\n",
    "    matplotlib.pyplot.plot(x_value, walks[i], linewidth=0.5)\r\n",
    "\r\n",
    "# 首次穿越时间\r\n",
    "hits30 = (numpy.abs(walks) >= 30).any(1)\r\n",
    "count_hits30 = hits30.sum()\r\n",
    "\r\n",
    "arr_info([hits30, count_hits30])"
   ],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "orig_nbformat": 4,
  "language_info": {
   "name": "python",
   "version": "3.9.6",
   "mimetype": "text/x-python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "pygments_lexer": "ipython3",
   "nbconvert_exporter": "python",
   "file_extension": ".py"
  },
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.6 64-bit ('DataAnalysis': venv)"
  },
  "interpreter": {
   "hash": "0fe90ac710750590bd916650f441b3b2233faa2658175d6302b22e90f225e38d"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}